//
//  FFShareView.m
//  FFMall
//
//  Created by IT.Bear on 16/6/22.
//  Copyright © 2016年 IT.Bear. All rights reserved.
//

#import "FFShareView.h"
#import "FFShareManager.h"
#import "WXApi.h"
#import "UMSocialQQHandler.h"

#import "UIImage+FFIconFont.h"
#import "Masonry.h"

@interface FFShareView ()
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIView *iconContainView;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;

//@property (weak, nonatomic) IBOutlet UIImageView *wxFriend;
//@property (weak, nonatomic) IBOutlet UIImageView *wxQuan;
//@property (weak, nonatomic) IBOutlet UIImageView *sinaImageView;
//@property (weak, nonatomic) IBOutlet UIImageView *qqImageView;
//@property (weak, nonatomic) IBOutlet UIImageView *ffcopyIcon;

@property (strong, nonatomic) UIImageView *wxFriend;
@property (strong, nonatomic) UIImageView *wxQuan;
@property (strong, nonatomic) UIImageView *sinaImageView;
@property (strong, nonatomic) UIImageView *qqImageView;
@property (strong, nonatomic) UIImageView *ffcopyIcon;

@property (nonatomic,copy) resultCallback callback;
@property (nonatomic ,strong) NSString *shareText;
@property (nonatomic ,strong) UIImage *shareImage;
@property (nonatomic ,strong) NSArray *shareToSnsNames;
@property (nonatomic ,strong) NSString *shareUrl;
@property (nonatomic ,strong) NSString *shareTitle;
@property (nonatomic ,strong) UIViewController *controller;

@property (nonatomic ,assign) BOOL isInstallSina;
@property (nonatomic ,assign) CGFloat iconWidth;
@property (nonatomic ,strong) UIView *iconView;
@property (nonatomic ,assign) CGRect myFrame;

@end
@implementation FFShareView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"FFShareView" owner:nil options:nil] firstObject];
        _myFrame = frame;
        [_cancelBtn setImage:[UIImage iconWithIcon:@"\U0000e6fa" fontSize:28 color:[UIColor blackColor]] forState:UIControlStateNormal];
        _iconView = [[UIView alloc] init];
        [_iconContainView addSubview:_iconView];
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_iconContainView.mas_centerX);
            make.top.mas_equalTo(_iconContainView.mas_top);
            make.bottom.mas_equalTo(_iconContainView.mas_bottom);
        }];
        _sinaImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"videoShareWb"]];
//        [_iconView addSubview:_sinaImageView];
        
        //是否安装微信
        if([WXApi isWXAppInstalled]){
            _wxFriend = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"videoShareWx"]];
            [_iconView addSubview:_wxFriend];
            
            _wxQuan = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"videoSharePyq"]];
            [_iconView addSubview:_wxQuan];
        
        }
        //是否安装qq
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
            _qqImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"videoShareQQ"]];
            [_iconView addSubview:_qqImageView];
        }
        
        
//        _ffcopyIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"videoShareCopy"]];
//        [_iconView addSubview:_ffcopyIcon];

        _qqImageView.userInteractionEnabled = YES;
        _wxQuan.userInteractionEnabled = YES;
        _wxFriend.userInteractionEnabled = YES;
        _sinaImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareToWerchatFriend)];
        [_wxFriend addGestureRecognizer:tap1];
        
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareToWerchatFriendQuan)];
        [_wxQuan addGestureRecognizer:tap2];
        
//        UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareToQQ)];
//        [_qqImageView addGestureRecognizer:tap3];
//
//        UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareToSina)];
//
//        [_sinaImageView addGestureRecognizer:tap4];
        
//        UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionCopy)];
//        [_ffcopyIcon addGestureRecognizer:tap5];
        [self addTap];

    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    self.frame = _myFrame;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
//    [self loadUI];
}

- (void)loadUI
{
        //是否安装新浪
    CGFloat iconSizeHeight = FIT_FLOAT(35);
    CGFloat space = 29;
    CGFloat width = FIT_FLOAT(35) + space;
    _iconWidth = 0;
//    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"FFSina://wb1260440367"]]) {
//        _isInstallSina = YES;
//    }else{
//        _isInstallSina = NO;
//
//    }
//    _iconWidth += width;
//
//    [_sinaImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(_iconView.mas_left);
//        make.top.mas_equalTo(_iconView.mas_top).offset(8);
//        make.height.mas_equalTo(iconSizeHeight);
//        make.width.mas_equalTo(iconSizeHeight);
//    }];
    
    //是否安装微信
    if (_wxFriend && _wxQuan) {
        _wxFriend.hidden = NO;
        _wxQuan.hidden = NO;

        [_wxFriend mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_iconView).offset(_iconWidth);
            make.top.mas_equalTo(_iconView.mas_top).offset(8);
            make.height.mas_equalTo(iconSizeHeight);
            make.width.mas_equalTo(iconSizeHeight);
        }];
        
        _iconWidth += width*2;
        
        [_wxQuan mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_iconView).offset(_iconWidth - width);
            make.top.mas_equalTo(_iconView.mas_top).offset(8);
            make.height.mas_equalTo(iconSizeHeight);
            make.width.mas_equalTo(iconSizeHeight);
        }];
    }else{
        _wxFriend.hidden = YES;
        _wxQuan.hidden = YES;

    }
    
    //是否安装qq
    if (_qqImageView) {
        _qqImageView.hidden = NO;
        
        [_qqImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_iconView.mas_left).offset(_iconWidth);
            make.top.mas_equalTo(_iconView.mas_top).offset(8);
            make.height.mas_equalTo(iconSizeHeight);
            make.width.mas_equalTo(iconSizeHeight);
            
        }];
        _iconWidth += width;
        
    }else{
        _qqImageView.hidden = YES;
    }
    
    //copyIcon
//    if (_shareUrl.length) {
//        [_ffcopyIcon mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(_iconView.mas_left).offset(_iconWidth);;
//            make.top.mas_equalTo(_iconView.mas_top).offset(8);
//            make.height.mas_equalTo(iconSizeHeight);
//            make.width.mas_equalTo(iconSizeHeight);
//        }];
//        _iconWidth += iconSizeHeight;
//        _ffcopyIcon.hidden = NO;
//    }else{
        _iconWidth -= space;
        _ffcopyIcon.hidden = YES;
//    }
    
    [_iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_iconContainView.mas_centerX);
        make.top.mas_equalTo(_iconContainView.mas_top);
        make.bottom.mas_equalTo(_iconContainView.mas_bottom);
        make.width.mas_equalTo(_iconWidth);
    }];
}

//- (void)actionCopy
//{
//
//    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
//
//    pasteboard.string = self.shareUrl;
//
//    [[FFLoadingTips sharedInstance] showTips:@"已复制链接到剪贴板"];
//}

- (void)addTap
{
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
    [self.mainView addGestureRecognizer:tap2];
}
////转发
//- (IBAction)Forwarding:(id)sender {
//
//}

//取消
- (IBAction)cancel:(id)sender {
    [self dismiss];
}

//分享给微信好友
- (void)shareToWerchatFriend
{
    [self shareToSnaName:UMSocialPlatformType_WechatSession];
}

- (void)shareToWerchatFriendQuan
{
    [self shareToSnaName:UMSocialPlatformType_WechatTimeLine];
}

- (void)shareToSina
{
    if (!_isInstallSina) {
        [self dismiss];
    }
    [self shareToSnaName:UMSocialPlatformType_Sina];
}

- (void)shareToQQ
{
    [self shareToSnaName:UMSocialPlatformType_QQ];
}

- (void)shareToSnaName:(UMSocialPlatformType)shareToPlatformType
{
    //获取分享获得的优惠券编号
    [self shareActionPlat:shareToPlatformType];
}

- (void)shareActionPlat:(UMSocialPlatformType)shareToPlatformType
{
    WS(weakSelf);
//    if (self.shareUrl.length) {
//        [self getShorUrl:^(NSString *shortUrl) {
//            if (shortUrl.length) {
//                [[FFShareManager sharedInstance] shareToSnsSheetView:self.controller shareTitle:self.shareTitle shareText:self.shareText shareImage:self.shareImage shareToPlatformType:shareToPlatformType shareUrl:shortUrl callBack:^(BOOL sucessful, NSString *snsName) {
//                    if (weakSelf.callback) {
//                        [weakSelf dismiss];
//                        weakSelf.callback (sucessful , snsName);
//                    }
//                }];
//            }else{
//                [[FFShareManager sharedInstance] shareToSnsSheetView:self.controller shareTitle:self.shareTitle shareText:self.shareText shareImage:self.shareImage shareToPlatformType:shareToPlatformType shareUrl:self.shareUrl callBack:^(BOOL sucessful, NSString *snsName) {
//                    if (weakSelf.callback) {
//                        [weakSelf dismiss];
//                        weakSelf.callback (sucessful , snsName);
//                    }
//                }];
//            }
//        }];
//    }else{
        [[FFShareManager sharedInstance] shareToSnsSheetView:self.controller shareTitle:self.shareTitle shareText:self.shareText shareImage:self.shareImage shareToPlatformType:shareToPlatformType shareUrl:self.shareUrl callBack:^(BOOL sucessful, NSString *snsName) {
            if (weakSelf.callback) {
                [weakSelf dismiss];
                weakSelf.callback (sucessful , snsName);
            }
        }];

//    }
    
}

- (void)showWithSnsSheetView:(UIViewController *)controller
                  shareTitle:(NSString *)shareTitle
                   shareText:(NSString *)shareText
                  shareImage:(UIImage *)shareImage
                    shareUrl:(NSString *)shareUrl
                    callBack:(resultCallback)callBack
{
    
    self.controller = controller;
    self.shareText = shareText;
    self.shareImage = shareImage;
    self.shareUrl = shareUrl;
    self.callback = callBack;
    self.shareTitle = shareTitle;
    [self loadUI];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
//    self.backgroundColor = [UIColor ];
    
    __weak typeof(self) weakSelf = self;
    _mainView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, _mainView.frame.size.height);
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.mainView.frame = CGRectMake(0, self.frame.size.height - weakSelf.mainView.frame.size.height, self.frame.size.width, weakSelf.mainView.frame.size.height);
    } completion:^(BOOL finished) {
        ///
    }];
}

- (void)dismiss
{
    __weak typeof(self) weakSelf = self;
    
    
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        weakSelf.mainView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, weakSelf.mainView.frame.size.height);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
@end
